package ift605.tp3.b;

import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ConsoleAgent extends Agent
{
    private static final long serialVersionUID = 1L;
    
    @Override
    protected void setup()
    {
        System.out.println(getLocalName() + " is here, ready to have fun !!");
        try
        {
            final DFAgentDescription dfd = new DFAgentDescription();
            dfd.setName(getAID());
            DFService.register(this, dfd);
            
            addBehaviour(new CyclicBehaviour(this)
            {
                private static final long serialVersionUID = 1L;
                
                @Override
                public void action()
                {
                    final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                    try
                    {
                        System.err.println("<" + getLocalName() + "> Please write your equation: ");
                        final String command = in.readLine();
                        if (command.equalsIgnoreCase("exit"))
                        {
                            final ACLMessage exit = new ACLMessage(ACLMessage.CANCEL);
                            exit.addReceiver(new AID("Dispatcher", AID.ISLOCALNAME));
                            send(exit);
                            doDelete();
                        }
                        else
                        {
                            System.out.println("<" + getLocalName() + "> You just said: " + command);
                            final ACLMessage request = new ACLMessage(ACLMessage.REQUEST);
                            request.setContent(command);
                            request.addReceiver(new AID("Dispatcher", AID.ISLOCALNAME));
                            send(request);
                            final ACLMessage confirmation = blockingReceive(MessageTemplate.or(MessageTemplate.MatchPerformative(ACLMessage.CONFIRM), MessageTemplate.MatchPerformative(ACLMessage.DISCONFIRM)));
                            if (confirmation.getPerformative() == ACLMessage.DISCONFIRM)
                            {
                                System.err.println("<" + getLocalName() + "> Sorry, you are not getting anywhere with this");
                                return;
                            }
                            final ACLMessage result = blockingReceive(MessageTemplate.or(MessageTemplate.MatchPerformative(ACLMessage.FAILURE), MessageTemplate.MatchPerformative(ACLMessage.INFORM)));
                            if (result.getPerformative() == ACLMessage.FAILURE)
                            {
                                System.err.println("<" + getLocalName() + "> Sorry, you are not getting anywhere with this");
                                return;
                            }
                            System.err.println("<" + getLocalName() + "> Here is the result for [" + command + "]: " + result.getContent());
                        }
                    }
                    catch (final IOException e)
                    {
                        e.printStackTrace();
                    }
                }
            });
        }
        catch (final Exception e)
        {
            System.out.println("This is too much for this poor counselor: " + e);
            e.printStackTrace();
        }
    }
}
